# 成功

import requests,pymongo
from bs4 import BeautifulSoup
import re
# 链接数据库
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["demo_db"]          # 库
coll = db["quotes"]
l = []


for n in range(1,11):
    url = f'https://www.bqg128.cc/json?sortid=1&page={n}'
    headers = {
        "referer":"https://www.bqg128.cc/xuanhuan/",

        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"}
    response = requests.get(url,headers=headers,timeout=10)


    for item in response.json():

        articlename = item['articlename']
        url_img = item['url_img']
        l.append({articlename:url_img})

    # result = coll.insert_many(l)
    print(l)
#     print(l,type(l))
# for q in coll.find({}, limit=2):
#     print(q)



# name = re.findall(r'<img src="(.*?)" alt="(.*?)" width="120" height="150" border="0" />', response.text)
#
# print(name)
# for item in name:
#     print(item)